﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;

namespace RPAVIEW.util {
    internal class ElementUtil {
        public ElementUtil() {
            
        }

        //获取目标元素列表
        public static IEnumerable<T> GetTargetElementCollection<T>(DependencyObject obj) where T : DependencyObject { 
            if (obj == null) { 
                yield return null;
            }
    
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++){
                var child = VisualTreeHelper.GetChild(obj, i);

                if (child != null && child is T) { 
                    yield return (T)child;
                }
                foreach (T childOfChild in GetTargetElementCollection<T>(child)) { 
                    yield return childOfChild;
                }
            }
        }

        //获取窗口句柄
        public static IntPtr getWindowPtr(Window win) { 
            IntPtr hwnd = new WindowInteropHelper(win).Handle;
            return hwnd;
        }

        //获取控件句柄
        public static IntPtr GetControlsPtr(UIElement ele) {
            return ((HwndSource)PresentationSource.FromVisual(ele)).Handle;
        }

        public static void ShowRow(RowDefinition row) { 
            row.Height = GridLength.Auto;
        }

        public static void HideRow(RowDefinition row) { 
            row.Height = new GridLength(0);
        }

        //获取窗口IAccessable对象
        /*public static IAccessible GetWinAccessable(IntPtr winPtr) { 
            IAccessible acc;
            int result = User32Utils.AccessibleObjectFromWindow(winPtr, (long)ObjectTagEnum.OBJID_WINDOW, DefultContents.IID_IACCESSIBLE.ToByteArray(), out acc);
            if(result >= 0) {
                return acc;
            } else { 
                string errMsg = ConverterUtil.GetLastErrorMsg();
                //记录错误日志(等待完善日志系统)
                return null;
            }

        }

        //获取窗口IAccessable对象(通过坐标)
        public static IAccessible GetWinAccessable(int x, int y) { 
            IntPtr ptr = User32Utils.WindowFromPoint(new System.Drawing.Point(x, y));
            return GetWinAccessable(ptr);
        }*/
    }
}
